from flask import Blueprint, render_template, request, redirect, session
from utils import db

# 登录相关功能
# 蓝图对象
ac = Blueprint('account', __name__)


@ac.route('/login', methods=['GET', 'POST'])
def login():
    # return "登录"
    if request.method == 'GET':
        return render_template('login.html')

    # 获取表单数据
    role = request.form.get("role")
    mobile = request.form.get("mobile")
    pwd = request.form.get("pwd")
    print(role, mobile, pwd)

    user_dict = db.fetch_one("select * from userinfo where role=%s and mobile=%s and password=%s", [role, mobile, pwd])
    print(user_dict)
    if user_dict:
        # 登录成功 + 跳转
        session["user_info"] = {"role": role, 'real_name': user_dict['real_name'], 'id': user_dict['id']}
        return redirect('/order/stock')
    return render_template("login.html", error="用户名或密码错误")


@ac.route('/users')
def users():
    return "用户列表"
